//
// ==[ XPGL: eXPerimental Graphics Library ]== 
//
// Copyright 2006 JeGX / oZone3D.Net
// http://www.oZone3D.Net - jegx@ozone3d.net
//
// This SOFTWARE is distributed in the hope that it will be useful.
// TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
// *AS IS* AND oZone3D.Net DISCLAIM ALL WARRANTIES, EITHER EXPRESS
// OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL oZone3D.Net 
// BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
// WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
// BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
// ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF oZone3D.Net HAS
// BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 
//


#pragma once
#pragma warning(disable : 4996)
#include <float.h>
#include <limits.h>
#include <vector>
#include <string>

#define SAFE_DELETE( ptr ) \
	if(ptr) \
	{ \
		delete ptr; \
		ptr = NULL; \
	} \

#define SAFE_DELETE_ARRAY( ptr ) \
	if(ptr) \
	{ \
		delete [] ptr; \
		ptr = NULL; \
	} \

#define SAFE_DELETE_2DMATRIX(p, dim1) \
	if(p) \
{\
	for(unsigned i=0; i<dim1; ++i)\
{	delete[] p[i]; p[i] = NULL; }\
	delete[] p;\
	p= NULL; \
}\

void MSG_BOX_INFO( const char *strString, ... );
void MSG_BOX_WARNING( const char *strString, ... );
void MSG_BOX_ERROR( const char *strString, ... );
void MSG_BOX_ERROR_T( const char *strString, ... );

size_t ExecuteProcess(std::wstring FullPathToExe, std::wstring Parameters, size_t SecondsToWait);

//CString
CString GetNameFromPath(const CString &src);

CString GetFolderFromPath(const CString &src);

CString GetSuffixFromPath( const CString &src);
//file 
extern CString G_RootFolder;
extern bool G_bSetCurrentFoldAsInit;
void LoadingDialog(std::ifstream &stream, CString suffix, CString title, CString initDir="", CDialog *pDlg=NULL);

bool LoadingDialog(CString &fileName, CString suffix, CString title, CString initDir="", CDialog *pDlg=NULL);

void SavingDialog(std::ofstream &stream, CString suffix, CString title, CString initDir="", CDialog *pDlg=NULL);

bool SavingDialog(CString &fileName, CString suffix, CString title, CString initDir="", CDialog *pDlg=NULL);

void FindAllFile(CString strParent, CString ext, std::vector<CString> & pathList);

int	CALLBACK	BrowseCallbackProc(HWND hwnd, UINT msg, LPARAM lp, LPARAM pData);  

extern char G_BrowserStartPath[MAX_PATH+1];
void SetBrowserStartPath(char* s);

template<typename T>
bool LoadMatlabCsvFile(std::ifstream &stream, T *m, const unsigned nR,const unsigned nC) 
{
	std::locale NarrowCharStyle("C");
	stream.imbue(NarrowCharStyle);
	char c;
	for (unsigned i=0; i<nR; ++i){
		for(unsigned j=0; j<nC-1; ++j){
			stream >> m[i*nC + j] >> c;
		}
		stream >> m[i*nC+nC-1];
	}
	return true;
}

//Matlab Engine
#define PFN_BOOL_VOID  bool (* ) (void) 

#if MATLAB_AVAILABLE
#include "mclmcrrt.h"
#include "mclcppclass.h"
#pragma  comment( lib,		"mclmcrrt.lib")
bool initMatlab(std::vector<PFN_BOOL_VOID> &pf);	/* Start matlab Engine, then init the matlab func lib you need*/
void TerminateMatlab(); /*close matlab*/
#endif
